<template>
	<view class="content">
		<view class="write">
			<view class="write-box">
				<view class="">
					<uv-textarea v-model="evaluateText" placeholder="请对产品进行评价~"
						:custom-style="{ border: 'none' }"></uv-textarea>
				</view>
				<view class="Scoring">
					<view class="">
						<uv-rate count="5" touchable="true" v-model="value" @change="rateChange" inactiveColor="#daa03d"
							activeColor="#daa03d" size="20"></uv-rate>
					</view>
					<view class="size-12 color-666">
						{{ rateText }}
					</view>
				</view>
				<view class="">
					<uv-upload :fileList="fileList1" name="1" multiple :maxCount="10" @afterRead="afterRead"
						@delete="deletePic"></uv-upload>
				</view>
			</view>
			<view class="checkout-btn color-333">
				提交评价
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				evaluateText: '',
				fileList1: [],
				value: 0
			};
		},
		computed: {
			// 使用 computed 属性根据 value 返回对应文字
			rateText() {
				if (this.value <= 0) return '请选择评分';
				if (this.value <= 2) return '差评';
				if (this.value <= 4) return '中评';
				if (this.value === 5) return '好评';
				return '';
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			rateChange(e) {

				console.log(e);
			}

		}

	}
</script>

<style lang="less">
	.write {
		padding: 30rpx;

		.write-box {
			width: 100%;
			padding: 30rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			gap: 30rpx;

			.Scoring {
				display: flex;
				align-items: center;
				gap: 20rpx;
			}


		}

		.checkout-btn {
			width: 100%;
			background-image: linear-gradient(to right, #7cdfe8, #32b9b3);
			background-color: #32b9b3;
			border-radius: 50rpx;
			padding: 20rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: center;
			align-content: center;
			color: #fff;
			margin-top: 40rpx;
			
		}
	}
</style>